package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.system.api.model.LoginUser;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.domain.vo.UserInfoVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 会员Controller
 *
 * @author lee
 * @date 2024-07-23
 */
@Tag(name = "会员接口管理")
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {
    @Autowired
    private IUserInfoService userInfoService;

    @Operation(summary = "查询商品是否被收藏")
    @GetMapping("/isCollect/{skuId}")
    public AjaxResult isCollect(@PathVariable("skuId") Long skuId){
        Long userId = SecurityContextHolder.getUserId();
        return success(userInfoService.isCollect(skuId,userId));
    }

    @Operation(summary = "收藏")
    @GetMapping("/collect/{skuId}")
    public AjaxResult collect(@PathVariable("skuId") Long skuId){
        Long userId = SecurityContextHolder.getUserId();
        return success(userInfoService.collect(skuId,userId));
    }

    @Operation(summary = "取消收藏")
    @GetMapping("/cancelCollect/{skuId}")
    public AjaxResult cancelCollect(@PathVariable("skuId") Long skuId){
        Long userId = SecurityContextHolder.getUserId();
        return success(userInfoService.cancelCollect(skuId,userId));
    }
    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户登录信息")
    @GetMapping("/getLoginUserInfo")
    public R<UserInfoVo> getUserInfo(){
        Long userId = SecurityContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return R.ok(userInfoVo);
    }

    /**
     * 注册
     */
    @Operation(summary = "注册")
    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody UserInfo userInfo) {
        Integer register = userInfoService.register(userInfo);
        return register > 0 ? R.ok() : R.fail();
    }

    @Operation(summary = "根据用户名获取用户信息")
    @GetMapping("/userInfo")
    @InnerAuth
    public R<UserInfo> getUserInfo(@RequestParam("username") String username) {
        UserInfo userInfo = userInfoService.getUserInfoByUsername(username);
        return R.ok(userInfo);
    }

    @Operation(summary = "更新用户登录信息")
    @PutMapping("/updateUserLogin")
    @InnerAuth
    public R<Boolean> updateUserLogin(@RequestBody UpdateUserLogin updateUserLogin) {
        return userInfoService.updateUserLogin(updateUserLogin) == true ? R.ok(true) : R.ok(false);
    }


    /**
     * 查询会员列表
     */
    @Operation(summary = "查询会员列表")
    @RequiresPermissions("user:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(UserInfo userInfo) {
        startPage();
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @Operation(summary = "导出会员列表")
    @RequiresPermissions("user:userInfo:export")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserInfo userInfo) {
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    @Operation(summary = "获取会员详细信息")
    @RequiresPermissions("user:userInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userInfoService.getById(id));
    }

    /**
     * 新增会员
     */
    @Operation(summary = "新增会员")
    @RequiresPermissions("user:userInfo:add")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.save(userInfo));
    }

    /**
     * 修改会员
     */
    @Operation(summary = "修改会员")
    @RequiresPermissions("user:userInfo:edit")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.updateById(userInfo));
    }

    /**
     * 删除会员
     */
    @Operation(summary = "删除会员")
    @RequiresPermissions("user:userInfo:remove")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userInfoService.removeBatchByIds(Arrays.asList(ids)));
    }
}
